package com.hit.server.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.server.api.entity.RobotClientEntity;
import com.hit.server.api.dto.RobotClientQueryDTO;
import com.hit.server.api.dto.RobotClientSaveDTO;
import com.hit.server.api.dto.RobotClientUpdateDTO;
import com.hit.server.api.type.OnlineStatusEnums;
import com.hit.server.api.vo.RobotClientVO;
import com.hit.server.biz.mapper.RobotClientMapper;
import com.hit.server.api.service.RobotClientService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

/**
 * 执行端
 *
 * @author kehanjiang
 * @date 2025-04-03 17:25:01
 */
@Service
@RequiredArgsConstructor
public class RobotClientServiceImpl extends ServiceImpl<RobotClientMapper, RobotClientEntity> implements RobotClientService {

	private RobotClientVO convertToVO(Object o) {
        if (o instanceof RobotClientEntity) {
            RobotClientVO vo = new RobotClientVO();
            BeanUtil.copyProperties(o, vo);
			//设置其他参数
            Optional.ofNullable(vo.getOnlineStatus()).ifPresent(onlineStatus -> {
                vo.setOnlineStatusCn(
                        OnlineStatusEnums.getByCode(onlineStatus).getDesc()
                );
            });
            return vo;
        }
        return null;
    }
	
	private void preCheck(RobotClientEntity entity, Boolean update) {
        Long id = entity.getId();
        //String name = entity.getName();
        if (update) {//更新操作
           Assert.notNull(id, "id不可为空！");
        } else {
           //Assert.hasText(name, "name不可为空！");
        }
    }

    @Override
    public Page<RobotClientVO> getRobotClientPage(Page page, RobotClientQueryDTO dto){
        RobotClientEntity entity = new RobotClientEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setName(null);
        entity.setSn(null);
        final String name = dto.getName();
        final String sn = dto.getSn();
        LambdaQueryWrapper<RobotClientEntity> wrapper = new LambdaQueryWrapper<>(entity);
        wrapper.like(StringUtils.hasText(name), RobotClientEntity::getName, name);
        wrapper.like(StringUtils.hasText(sn), RobotClientEntity::getSn, sn);
        wrapper.orderByAsc(RobotClientEntity::getId);
        return (Page<RobotClientVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }
	
    @Override
    public RobotClientVO getRobotClientById(Long id){
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }
	
	
    @Override
    public Boolean saveRobotClient(RobotClientSaveDTO dto){
        RobotClientEntity entity = new RobotClientEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, false);
        return this.save(entity);
    }
	
    @Override
    public Boolean updateRobotClientById(RobotClientUpdateDTO dto){
        RobotClientEntity entity = new RobotClientEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, true);
        return this.updateById(entity);
    }
	
    @Override
    public Boolean removeRobotClientBatchByIds(List<Long> ids){
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }
}